package cn.jesin.ssmss01.utils;

import cn.hutool.core.util.StrUtil;

import java.util.regex.Pattern;

/**
 * @author 谷汉斌
 * @description 数据校验工具类
 * @createTime 2024/8/13 下午2:45
 */
public class DataCheckUtil {
    /**
     * 手机号检验
     */
    public static boolean isValidPhoneNumber(String phoneNumber) {
        if (StrUtil.isNotBlank(phoneNumber)) {
            return Pattern.matches("^1[3-9]\\d{9}$", phoneNumber);
        }
        return false;
    }

    /**
     * 邮箱校验
     */
    public static boolean isValidEmail(String email) {
        if ((email != null) && (!email.isEmpty())) {
            return Pattern.matches("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$", email);
        }
        return false;
    }

    /**
     * 根据类型校验
     */
    public static boolean isValid(String data, String type) {
        if (StrUtil.isNotBlank(type)) {
            return switch (type) {
                case "phone" -> isValidPhoneNumber(data);
                case "email" -> isValidEmail(data);
                default -> false;
            };
        }
        return false;
    }
}
